package icraf.carbon.gui;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;

import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.table.TableModel;

public class ClipboardPanel extends JPanel {
	class ClipboardTableModel extends MatrixTableModel {

		private static final long serialVersionUID = 8968868974903218229L;
		private String[][] data;
		private NumberRowHeader tableListModel = new NumberRowHeader(this);
		private String[] columnHeader;

		public ClipboardTableModel() {

		}

		@Override
		public Class<?> getColumnClass(int column) {
			return String.class;
		}

		@Override
		public int getColumnCount() {
			if (data != null && data.length > 0) {
				if(columnHeader != null) {
					return Math.min(data[0].length, columnHeader.length);
				}
				return data[0].length;
			}
			return 0;
		}

		@Override
		public String getColumnName(int col) {
			if(columnHeader != null) {
				return columnHeader[col];
			}
			return "column_" + Integer.toString(col);
		}

		public String[][] getData() {
			return data;
		}

		@Override
		public int getRowCount() {
			if(data == null) return 0;
			return data.length;
		}

		@Override
		public ListModel<Object> getRowHeaderModel() {
			return tableListModel;
		}

		@Override
		public Object getValueAt(int row, int column) {
			return data[row][column];
		}

		@Override
		public boolean isCellEditable(int row, int col) {
			return false;
		}

		public void setData(String[][] data) {
			this.data = data;
			fireTableStructureChanged();
			tableListModel.fireContentChange(0);
		}

		public String[] getColumnHeader() {
			return columnHeader;
		}

		public void setColumnHeader(String... columnHeader) {
			this.columnHeader = columnHeader;
		}

	}

	private static final long serialVersionUID = -6267507992297896197L;

	private ClipboardTableModel clipboardTableModel;
	private MatrixPanel clipboardTablePanel;
	private Clipboard system;
	private String[] fields;
	private int nColumn = 0;

	public ClipboardPanel() {
		super();
		clipboardTableModel = new ClipboardTableModel();
		clipboardTablePanel = new MatrixPanel(clipboardTableModel);
		this.setLayout(new BorderLayout());
		this.add(clipboardTablePanel);
	}

	public void updateClipboardData() {
		system = Toolkit.getDefaultToolkit().getSystemClipboard();
		try {
			String trstring = (String) (system.getContents(this)
					.getTransferData(DataFlavor.stringFlavor));
			String lineStr[] = trstring.split("\n");
			String val[] = lineStr[0].split("\t");
			nColumn = val.length;
			String[][] data = new String[lineStr.length][nColumn];
			for (int i = 0; i < lineStr.length; i++) {
				data[i] = lineStr[i].split("\t");
			}
			clipboardTableModel.setData(data);
			
		} catch (Exception ex) {
			//unsupported clipboard data
		}
	}

	public String[] getFields() {
		return fields;
	}

	public void setFields(String... fields) {
		this.fields = fields;
		clipboardTableModel.setColumnHeader(fields);
		clipboardTableModel.fireTableStructureChanged();
	}

	public TableModel getTableModel(){
		return clipboardTableModel;
	}
}